﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace S2_Xxxx_XxxNetApi
{
    public static class RedisHelper
    {
        public static string Constr = AppSettings.RedisConnectString;

        public static CSRedis.CSRedisClient Cache { get; set; }

        static RedisHelper()
        {
            BuildCache();
        }

        public static void SetCon(string config)
        {
            Constr = config;
        }

        public static void BuildCache()
        {
            try
            {
                if (Cache == null)
                {
                    Cache = new CSRedis.CSRedisClient(string.Format("{0},defaultDatabase=8,poolsize=50,ssl=false,writeBuffer=10240,prefix=S2", Constr));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("构建Redis异常：" + ex.Message);
                throw ex;
            }
        }

        public static void Add(string key, object value, string region = "", TimeSpan? expiry = null)
        {
            throw new NotImplementedException();
        }

        public static void Clear()
        {
            throw new NotImplementedException();
        }

        public static void Dispose()
        {
            throw new NotImplementedException();
        }

        public static bool Exists(string key, string region = "")
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 设置Redis缓存，并json序列号
        /// </summary>
        /// <param name="key"></param>
        /// <param name="filed"></param>
        /// <param name="value"></param>
        public static void HashSet(string key, string filed, object value)
        {
            string _value = Convertor.ObjectToJson(value);
            Cache.HashSet(key, filed, _value);
        }

        /// <summary>
        /// 设置Redis缓存，并json序列号和超时时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="filed"></param>
        /// <param name="value"></param>
        /// <param name="expire"></param>
        public static void HashSet(string key, string filed, object value, TimeSpan expire)
        {
            string _value = Convertor.ObjectToJson(value);
            Cache.HashSetExpire(key, expire, filed, _value);
        }

        /// <summary>
        /// 设置Redis缓存,不转换json
        /// </summary>
        /// <param name="key"></param>
        /// <param name="filed"></param>
        /// <param name="value"></param>
        public static void HashSet(string key, string filed, string value)
        {
            Cache.HashSet(key, filed, value);
        }

        /// <summary>
        /// 设置Redis缓存,不转换json，设置过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="filed"></param>
        /// <param name="value"></param>
        /// <param name="expire"></param>
        public static void HashSet(string key, string filed, string value, TimeSpan expire)
        {
            Cache.HashSetExpire(key, expire, filed, value);
        }

        public static T HashGet<T>(string key, string filed)
        {
            string value = Cache.HashGet(key, filed);
            return Convertor.JsonToObject<T>(value);

        }

        public static string HashGet(string key, string filed)
        {
            string value = Cache.HashGet(key, filed);
            return value;

        }

        public static void HashRemove(string key, string filed)
        {
            Cache.HashDelete(key, filed);
        }

        public static bool HashExists(string key, string field)
        {
            return Cache.HashExists(key, field);
        }

        public static Dictionary<string, string> Get(string key)
        {
            Dictionary<string, string> s = Cache.HashGetAll(key);
            return s;
        }

        public static string Get(string key, string filed)
        {
            string s = Cache.HashGet(key, filed);
            return s;
        }

        public static void Remove(string key, string region = "")
        {
            throw new NotImplementedException();
        }

        public static void RemoveRegion(string region = "")
        {
            throw new NotImplementedException();
        }

        public static void Update(string key, string region, object data)
        {
            throw new NotImplementedException();
        }

        public static void Update(string key, object data)
        {
            throw new NotImplementedException();
        }
    }
}
